var assign = require('lodash/assign');

module.exports = {

  build:function(actionName,argsObj){
    //phantom 入口: www.js
    var args = ['www.js',actionName];

    Object.keys(argsObj).map(function (k) {
      var v = argsObj[k];

      var argPre = '-' + k;
      var argValue = v ? v : true;

      args.push(argPre);
      args.push(argValue);
    });

    return args;
  },
  parse:function(actionArgs){
    var paramRegExp = /^-[\w]+$/;

    var actionArgsTrans = [];

    actionArgs.forEach(function (argOne,i) {
      if(paramRegExp.test(argOne)){

        var key = argOne.replace('-','');
        var value = actionArgs[i+1] === undefined ? true:
          paramRegExp.test(actionArgs[i+1]) ? true:actionArgs[i+1];

        value = value === 'true'?true:
          value === 'false'?false:
            value === 'null'?null:value;

        var argObj = {};

        argObj[key] = value;

        actionArgsTrans.push(argObj);
      }
    });

    actionArgsTrans = actionArgsTrans.reduce(function (init,next) {
      return assign(init,next);
    },{});

    return actionArgsTrans;
  }
};